﻿using System;
using System.Collections.Generic;
using HiTeachers.Data.Manager;
using HiTeachers.Entities;
using HiTeachers.Interfaces;
using HiTeachers.Utilities;
using NHibernate;

namespace HiTeachers.Data
{
    public class ContactedRequestDao : AbstractNHibernateDao<ContactedRequest, Guid>, IContactedRequestDao
    {
        public ContactedRequest GetContactedRequest(Guid teacherId, Guid requestId)
        {
            try
            {
                const string hsql = "from ContactedRequest ctr where ctr.Teacher.ID = :teacherId and ctr.Request.ID = :requestId";

                IQuery query = NHibernateSession.CreateQuery(hsql);
                query.SetGuid("teacherId", teacherId);
                query.SetGuid("requestId", requestId);

                return query.UniqueResult<ContactedRequest>();
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return null;
            }
        }

        public IList<ContactedRequest> GetContactedRequests(Guid teacherId)
        {
            try
            {
                const string hsql = "from ContactedRequest ctr where ctr.Teacher.ID = :teacherId and ctr.IsDeleted = 0";

                IQuery query = NHibernateSession.CreateQuery(hsql);
                query.SetGuid("teacherId", teacherId);

                return query.List<ContactedRequest>();
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return null;
            }
        }

        public IList<Teacher> GetContactedTeachers(Guid stdRequestId)
        {
            try
            {
                const string hsql = "select ctr.Teacher from ContactedRequest ctr where ctr.Request.ID = :requestId and ctr.IsDeleted = 0";

                IQuery query = NHibernateSession.CreateQuery(hsql);
                query.SetGuid("requestId", stdRequestId);

                return query.List<Teacher>();
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return null;
            }
        }
    }
}
